//
//  CATransformLayerViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/14.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CATransformLayerViewController.h"
#import "UIDevice+XJDevice.h"

@interface CATransformLayerViewController ()

@property (nonatomic, strong) UIView *containerView;

@end

@implementation CATransformLayerViewController

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIDevice setOrientation:UIInterfaceOrientationPortrait];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width)];
    self.containerView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_containerView];
    
    CATransform3D transform3D = CATransform3DIdentity;
    transform3D.m34 = - 1.0 / 500.0;
    self.containerView.layer.sublayerTransform = transform3D;
    
    // 在containerView.layer上面添加CATransformLayer
    CATransform3D transform3DOne = CATransform3DIdentity;
    transform3DOne = CATransform3DTranslate(transform3DOne, -100, 0, 0);
//    transform3DOne = CATransform3DRotate(transform3DOne, -M_PI_4 / 5, 0, 1, 0);
    CALayer *cube1Layer = [self createCubeWith3DTranform:transform3DOne];
    [self.containerView.layer addSublayer:cube1Layer];
    
    CATransform3D transform3DTwo = CATransform3DIdentity;
    transform3DTwo = CATransform3DTranslate(transform3DTwo, 100, 0, 0);
    transform3DTwo = CATransform3DRotate(transform3DTwo, -M_PI_4, 1, 0, 0);
    transform3DTwo = CATransform3DRotate(transform3DTwo, -M_PI_4, 0, 1, 0);
    CALayer *cube2Layer = [self createCubeWith3DTranform:transform3DTwo];
    [self.containerView.layer addSublayer:cube2Layer];
    
    // ⚠️也许运行程序你会发现，左边的正方体怎么可以看到右面的边？右边的视图不是绕Y轴旋转45度么，怎么实际旋转大于45度？正常应该是左面的正方体只能看到正面的边，右面的正方体绕Y轴旋转45度。原因是因为我们的CATransformLayer是添加在containerView的上面的，因此CATransformLayer共享一个灭点，即屏幕中心点，这个就可以解释上面的现象了。你也可以手动的将containerView的m34注释掉，这时你发现就正常了，但是当我们使用3D旋转操作的时候必须设置灭点。不懂灭点的看一下前面的demo示例，里面讲解的很清楚。
    
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    returnButton.frame = CGRectMake(100, 300, 200, 30);
    returnButton.backgroundColor = [UIColor cyanColor];
    [returnButton setTitle:@"返回" forState:UIControlStateNormal];
    [returnButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(return:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:returnButton];
}

- (void)return:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 创建CATransformLayer
- (CALayer *)createCubeWith3DTranform:(CATransform3D)transform
{
    // 创建CATransformLayer，虚拟层，打开文档我们发现没有任何属性
    CATransformLayer *cubeTransformLayer = [CATransformLayer layer];
    
    // 下面开始在CATransformLayer上添加6个面
    //add cube face 1
    CATransform3D ct = CATransform3DMakeTranslation(0, 0, 50);
    [cubeTransformLayer addSublayer:[self addFaceWith3DTransform:ct]];
    
    //add cube face 2
    ct = CATransform3DMakeTranslation(50, 0, 0);
    ct = CATransform3DRotate(ct, M_PI_2, 0, 1, 0);
    [cubeTransformLayer addSublayer:[self addFaceWith3DTransform:ct]];
    
    //add cube face 3
    ct = CATransform3DMakeTranslation(0, -50, 0);
    ct = CATransform3DRotate(ct, M_PI_2, 1, 0, 0);
    [cubeTransformLayer addSublayer:[self addFaceWith3DTransform:ct]];
    
    //add cube face 4
    ct = CATransform3DMakeTranslation(0, 50, 0);
    ct = CATransform3DRotate(ct, -M_PI_2, 1, 0, 0);
    [cubeTransformLayer addSublayer:[self addFaceWith3DTransform:ct]];
    
    //add cube face 5
    ct = CATransform3DMakeTranslation(-50, 0, 0);
    ct = CATransform3DRotate(ct, -M_PI_2, 0, 1, 0);
    [cubeTransformLayer addSublayer:[self addFaceWith3DTransform:ct]];
    
    //add cube face 6
    ct = CATransform3DMakeTranslation(0, 0, -50);
    ct = CATransform3DRotate(ct, M_PI, 0, 1, 0);
    [cubeTransformLayer addSublayer:[self addFaceWith3DTransform:ct]];
    
    CGSize containerSize = self.containerView.frame.size;
    cubeTransformLayer.position = CGPointMake(containerSize.width / 2.0, containerSize.height / 2.0);
    cubeTransformLayer.transform = transform;
    NSLog(@"cubeTransformLayer的frame：%@", [NSValue valueWithCGRect:cubeTransformLayer.frame]); // 我们打印cubeTransformLayer，发现它是没有宽度和高度的，这个层就是一个虚拟层，它只是扮演了存储了其他的CALayer的容器，因此没有宽度和高度(其实你可把它想象成云盘，云盘可以存储数据，但是云盘是没有具体的实物大小的吧)
    return cubeTransformLayer;
}

- (CALayer *)addFaceWith3DTransform:(CATransform3D)transform
{
    CALayer *face = [[CALayer alloc] init];
    face.frame = CGRectMake(-50, -50, 100, 100); // 起始位置必须为-face.width/2，和-face.height/2
    CGFloat red = (arc4random() % 256) / 255.0;
    CGFloat green = (arc4random() % 256) / 255.0;
    CGFloat blue = (arc4random() % 256) / 255.0;
    face.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1].CGColor;
    face.transform = transform;
    return face;
}

@end
